package rcs34.android.photopublishare;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;

public class ActivityPublishare extends Activity {
	private final String LOGTAG = getClass().getName();
	private final int REQUEST_FROM_CAMERA = 1;
	private final int REQUEST_FROM_GALLERY = 2;
	private File photoFile;
	private Uri photoUri;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_publishare);
	}
	
	private boolean isIntentAvailable(Intent intent) {
		final PackageManager mgr = getPackageManager();
		return mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
		    .size() > 0;
	}
	
	public void handleFromCamera(View _view) {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		if (isIntentAvailable(intent)) {
			String storageState = Environment.getExternalStorageState();
			if (storageState.equals(Environment.MEDIA_MOUNTED)) {
				GregorianCalendar today=new GregorianCalendar();
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
				photoFile = new File(Environment.getExternalStorageDirectory()
						+File.separator+getText(R.string.app_name)+File.separator+format.format(today.getTime())+".jpg");
				try {
					if (photoFile.exists()) {
						photoFile.delete();
					}
					photoFile.getParentFile().mkdirs();
					photoFile.createNewFile();
					
				} catch (IOException e) {
					Log.e(LOGTAG, "Could not create file.", e);
				}
				photoUri = Uri.fromFile(photoFile);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
				startActivityForResult(intent, REQUEST_FROM_CAMERA);
			} else {
				new AlertDialog.Builder(this)
				    .setMessage(
				        "External Storeage (SD Card) is required.\n\nCurrent state: "
				            + storageState).setCancelable(true).create().show();
			}
		} else {
			new AlertDialog.Builder(this).setTitle("No camera")
			    .setMessage("There is no camera application to take the picture")
			    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
				    public void onClick(DialogInterface arg0, int arg1) {
				    }
			    }).show();
		}
	}
	
	public void handleFromGallery(View _view){
		Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
         
        startActivityForResult(i, REQUEST_FROM_GALLERY);
	}
	
	public void handleSettings(View _view){
		Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
         
        startActivityForResult(i, REQUEST_FROM_GALLERY);
	}
	 
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent _intent) {
		if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_FROM_CAMERA) {
			Intent intent = new Intent();
			intent.setAction(GApp.ACTION_SEND_CAPTURED_IMAGE);
			intent.setType("image/jpg");
			intent.putExtra(Intent.EXTRA_STREAM,photoUri);
			intent.setClass(this, ActivityPreview.class);
			startActivity(intent);
		}else if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_FROM_GALLERY) {
			Intent intent = new Intent();
			intent.setType("image/jpg");
			intent.setAction(Intent.ACTION_SEND);
			intent.putExtra(Intent.EXTRA_STREAM,_intent.getData());
			intent.setClass(this, ActivityPreview.class);
			startActivity(intent);
		}
	}
}
